package com.appcan.sync.saleorder;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.Query;

import com.appcan.base.BaseBean;
import com.appcan.base.BusinessException;
import com.appcan.base.StringUtil;



@Stateless
@Local(SaleBo.class)
@Remote(SaleBo.class)
public class SaleBean extends BaseBean implements SaleBo {

	private SaleDAO dao = null;
	
	@PostConstruct
	public void init() {
		super.init();
		dao = new SaleDAO(getManager(this.getClass()));
	}
	
	@Override
	public Map<String ,SaleVO> quteryData(String billtype) {
		Map<String ,SaleVO> map = new HashMap<String,SaleVO>();
		StringBuffer sb = new StringBuffer();
		sb.append(" select shipment_number  pk_bill,");
		sb.append("  CREATED_BY operatorcode,ORGANIZATION_ID||SUBINVENTORY_CODE house , ");
		sb.append("  SEGMENT1 invbasdoccode,abs(PRIMARY_QUANTITY) num,TRANSACTION_DATE  dbilldate,ATTRIBUTE4 cubasdoc ,");
		sb.append("  '' pk_psn,'' pk_dept,ATTRIBUTE5 price,TRANSACTION_ID tranferid,TRANSACTION_TYPE_ID typeid ");
		sb.append("  from VV.VV_NCIMPORT_TRANSACTIONS_TMP where    nvl(IMPORTED_FLAG,'N')='N' ");
		sb.append(" and TRANSACTION_TYPE_ID ").append(StringUtil.inSql(billtype, "4"));
		Query query = this.getManager().createNativeQuery(sb.toString());
		query.setFirstResult(0);
		query.setMaxResults(100000000);
		List<Object[]> result = query.getResultList();
		for(Object[] o: result){
			if(map.get(String.valueOf(o[0]))==null){
				SaleVO vo = new SaleVO();
				SaleHeadVO head = new SaleHeadVO();
				head.setPk_bill(String.valueOf(o[0]==null?"":o[0]));
				head.setOperatorcode(String.valueOf(o[1]==null?"":o[1]));
				head.setHouse(String.valueOf(o[2]==null?"":o[2]));
				head.setDbilldate(String.valueOf(o[5]==null?"":o[5]));
				head.setCubasdoc(String.valueOf(o[6]==null?"":o[6]));
				head.setPk_psn(String.valueOf(o[7]==null?"":o[7]));
				head.setDeptcode(String.valueOf(o[8]==null?"":o[8]));
				head.setType(String.valueOf(o[11]==null?"":o[11]));
				vo.setHead(head);
				SaleItemVO  item = new SaleItemVO();
				item.setInvbasdoccode(String.valueOf(o[3]==null?"":o[3]));
				item.setNum(String.valueOf(o[4]==null?"":o[4]));
				item.setTranferid(String.valueOf(o[10]==null?"":o[10]));
				item.setDbilldate(String.valueOf(o[5]==null?"":o[5]));
				item.setPrice(String.valueOf(o[9]==null?"":o[9]));
				vo.getBody().add(item);
				map.put(String.valueOf(o[0]), vo);
			}else{
				SaleItemVO  item = new SaleItemVO();
				item.setInvbasdoccode(String.valueOf(o[3]==null?"":o[3]));
				item.setNum(String.valueOf(o[4]==null?"":o[4]));
				item.setPrice(String.valueOf(o[9]==null?"":o[9]));
				item.setTranferid(String.valueOf(o[10]==null?"":o[10]));
				item.setDbilldate(String.valueOf(o[5]==null?"":o[5]));
				map.get(String.valueOf(o[0])).getBody().add(item);
			}
		}
		return map;
	}

	@Override
	public void updateData(Map map,String typeCode) throws BusinessException {
		StringBuffer st = new StringBuffer();
		st.append(" update VV.VV_NCIMPORT_TRANSACTIONS_TMP set IMPORTED_FLAG='Y' where ");
		st.append("  TRANSACTION_TYPE_ID ").append(StringUtil.inSql(typeCode, "4"));
		st.append(" and  TRANSACTION_ID in (");
		Iterator<Map.Entry> it = map.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry<String, Map> entry = it.next();
			if("0".equals(entry.getValue().get("flag")))
				 st.append("'").append(entry.getKey()).append("',");
		}
		st.append("'')");
		Query query = this.getManager().createNativeQuery(st.toString());
		query.executeUpdate();
		
	}
	
	
}
